package com.qkl.xiatang.app.dto.feed;

import com.qkl.xiatang.app.entity.feed.FeedAllocConfig;
import com.qkl.xiatang.app.entity.user.AppUserInfo;
import com.qkl.xiatang.app.utils.AppUtils;
import com.qkl.xiatang.app.utils.appenum.FeedAllocStatusEnum;
import lombok.Data;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

@Data
public class FeedAllocAddWebDTO extends FeedAllocAddBaseDTO {

    private List<FeedAllocWebDTO> allocList;


    @Override
    public boolean checkParams() {
        if( !this.checkAllocList()){
            return false;
        }
        return checkParams2();
    }

    @Override
    public List<String> getFeedIds() {
        List<String> feedIdList = new ArrayList<>();
        List<FeedAddDTO> feedDTOList = this.getFeedDTOList();
        for (FeedAddDTO feedDTO : feedDTOList){
            feedIdList.add(feedDTO.getFeedId());
        }
        feedIdList = AppUtils.stringsDistinct(feedIdList);
        return feedIdList;
    }

    public List<FeedAddDTO> getFeedDTOList(){
        List<FeedAddDTO> feedDTOList = new ArrayList<>();
        for (FeedAllocWebDTO allocWebDTO : allocList){
            feedDTOList.addAll(allocWebDTO.getFeedList());
        }
        return feedDTOList;
    }

    private boolean checkAllocList(){
        if(ObjectUtils.isEmpty(allocList)){
            return false;
        }
        for (FeedAllocWebDTO allocWebDTO : allocList){
            if(null == allocWebDTO.getSerialNo()){
                return false;
            }
            if( !this.checkFeedList(allocWebDTO.getFeedList())){
                return false;
            }
        }
        return true;
    }

    private boolean checkFeedList(List<FeedAddDTO> feedList){
        if(ObjectUtils.isEmpty(feedList)){
            return false;
        }
        for (FeedAddDTO addDTO : feedList){
            if(ObjectUtils.isEmpty(addDTO.getFeedId())){
                return false;
            }
        }
        return true;
    }


    public List<FeedAllocDetailDTO> toDetailList(AddDTO addDTO, List<FeedAllocConfig>  configList, List<FeedAllocQtyDTO> feedAllocQtyList){
       String userId = addDTO.getUserInfo().getId();
       String userName = addDTO.getUserInfo().getName();
        List<FeedAllocDetailDTO> detailDTOS = new ArrayList<>();
        for(FeedAllocConfig config : configList) {
            Date feedAllocDate = config.toAllocDate(addDTO.getAllocDate());
            //Date nextAllocDate = AppUtils.addDays(allocDate,1);
            FeedAllocDetailDTO detailDTO = new FeedAllocDetailDTO();
            String detailId = AppUtils.genIdStr(detailDTO);
            detailDTO.setId(detailId);
            detailDTO.setAllocUserId(userId);
            detailDTO.setAllocUserName(userName);
            detailDTO.setPondId(addDTO.getPondId());
            detailDTO.setCompanyId(addDTO.getCompanyId());
            detailDTO.setName(config.getName());
            detailDTO.setSerialNo(config.getSerialNo());
            //detailDTO.setCheckSeparTime(checkSeparTime);
            detailDTO.setCurrAllocDate(addDTO.getAllocDate());
            detailDTO.setStatus(FeedAllocStatusEnum.NOT.getStatus());
            detailDTO.setMemo( StringUtils.trimToEmpty(this.getMemo()));
            detailDTO.setAllocDate(feedAllocDate);
            detailDTO.setStartTime( config.toStartTime(feedAllocDate));
            detailDTO.setEndTime( config.toEndTime(feedAllocDate));
            detailDTO.setTemplateId(addDTO.getTemplateId());
            for(FeedAllocWebDTO webDTO : allocList){
                if(webDTO.getSerialNo().equals(config.getSerialNo())){
                    detailDTO.setFeedId(feedSort(webDTO.getFeedList()));
                    addQtyList(detailDTO,webDTO.getFeedList(),feedAllocQtyList);
                }
            }
            detailDTOS.add(detailDTO);
        }
        return detailDTOS;
    }

    private static void addQtyList(FeedAllocDetailDTO detailDTO,List<FeedAddDTO> feedAddList,List<FeedAllocQtyDTO> feedAllocQtyList){
        if(ObjectUtils.isEmpty(feedAddList)){
            detailDTO.setQty(0.0);
            detailDTO.setStatus(FeedAllocStatusEnum.UN_NEED.getStatus());
            return;
        }
        Double qty = 0.0;
        String companyId = detailDTO.getCompanyId();
        String pondId = detailDTO.getPondId();
        Integer serialNo = detailDTO.getSerialNo();
        Date allocDate = detailDTO.getAllocDate();
        for(FeedAddDTO feedAddDTO : feedAddList){
            FeedAllocQtyDTO qtyDTO = new FeedAllocQtyDTO();
            qtyDTO.setId(AppUtils.genIdStr(qtyDTO));
            qtyDTO.setQty( (null == feedAddDTO.getQty() ? 0:feedAddDTO.getQty() ));
            qtyDTO.setFeedId(feedAddDTO.getFeedId());
            qtyDTO.setAllocDate(allocDate);
            qtyDTO.setPondId(pondId);
            qtyDTO.setCompanyId(companyId);
            qtyDTO.setSerialNo(serialNo);

            feedAllocQtyList.add(qtyDTO);
            qty = AppUtils.addAmt(qty,feedAddDTO.getQty());
        }
        detailDTO.setQty(qty);
        if(qty == 0.0){
            detailDTO.setStatus(FeedAllocStatusEnum.UN_NEED.getStatus());
        }
        if(feedAddList.size() > 1 ){
            detailDTO.setFeedNameLog("混合");
        }
    }

    private static String feedSort(List<FeedAddDTO> feedAddDTOList){
        List<String> feedIdList = new ArrayList<>();
        for (FeedAddDTO addDTO : feedAddDTOList){
            feedIdList.add(addDTO.getFeedId());
        }
        return feedIdSort(feedIdList);
    }

    private static String feedIdSort(List<String> feedIdList){
        Collections.sort(feedIdList);
        return String.join(",", feedIdList);
    }

    @Data
    public static class AddDTO{
        private AppUserInfo userInfo;
        private Date allocDate;
        private String pondId;
        private String templateId;
        private String companyId;

        public static AddDTO newDTO(AppUserInfo userInfo,String companyId, String templateId,String pondId,Date allocDate){
            AddDTO dto = new AddDTO();
            dto.setAllocDate(allocDate);
            dto.setPondId(pondId);
            dto.setCompanyId(companyId);
            dto.setTemplateId(templateId);
            dto.setUserInfo(userInfo);
            return dto;
        }

    }

}
